package school30;


import school30.ui.*;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


class MainCanvas extends Canvas
        implements Runnable
{
  //private int[] buffer; (Not actual anymore)
  private static Image mainImage;
  private static Graphics mainGraphics;
  Display display; // the display for the application

  public MainCanvas(Display d,int[] rgbData) {
    display = d;
    //buffer = rgbData;
  }

  //To be removed soon
  /*int[] getBuffer() {
     return buffer;
  }*/

// paint the current frame
  public void paint( Graphics g )
  { 
//    System.out.println("paint is called");

    /*g.drawRGB(buffer,
            0,  // offset
            getWidth(), //scan
            0,
            0,
            getWidth(),
            getHeight(),
            false);*/
    g.drawImage(mainImage, 0, 0, 0);
  }

  public void startAnimation()
  {
// set up initial frame
    repaint();
    display.callSerially(this);
  }
// called after previous repaint is finished
  public void run()
  {
    if (true)
    {
// set up the next frame
      repaint();
      display.callSerially(this);
    }
  }

  // is called by the platform when shown
  protected void showNotify() {
    shown = true;
    System.out.println("shown");

    if (mainImage==null)
    {
      mainImage = Image.createImage(getWidth(), getHeight());
      mainGraphics = mainImage.getGraphics();
    }


    /*if (buffer == null) {
        buffer = new int[getWidth()*getHeight()];
    }*/
    

  }

  protected void hideNotify() {
    shown = false;
    System.out.println("hidden");
  }

  static Graphics getMainGraphics() {
     return mainGraphics;
  }

  protected void keyPressed(int keyCode) {
    MainMIDlet.mainMIDlet.keyPressed (keyCode);
  }

  protected void keyReleased(int keyCode) {
    MainMIDlet.mainMIDlet.keyReleased (keyCode);
  }
  boolean shown=false;
}
